package collisionSystem;

import java.lang.Math;

import collisionPrimitives.*;
import myUtils.vec2;

public class CCollision 
{
	public static boolean PointIncCircle(CCircle circle, vec2 point) 
	{
		// Get the squared distance of the vector from the center of the circle to the point.
		// Optimized: Finding the squared distance avoids the use of the square root operator. 
		float dist = circle.center.distanceSq(point);
		
		// if the squared distance of the vector is less than the squared radius of the circle,
		// then we have a collision and this return true.
		return (Math.abs(dist) < (circle.radius * circle.radius));
	}
	
	public static boolean SphereToAABB(CCircle circle, CRect rec, vec2 closestPT) 
	{
		vec2 temp = circle.center;
		
		// These four if checks will set 'temp' as the point on the AABB that is
		// closest to the center of the circle. We can then do a point in circle collision
		// check to determine if the circle is colliding with the AABB.
		// Clamping the 'X' value of the point
		if (temp.getX() > rec.max.getX())
			temp.setX(rec.max.getX());
		if (temp.getX() < rec.min.getX())
			temp.setX(rec.min.getX());
		//Clamping the 'Y' value of the point
		if (temp.getY() > rec.max.getY())
			temp.setY(rec.max.getY());
		if (temp.getY() < rec.min.getY())
			temp.setY(rec.min.getY());
		
		// If needed, this function will give the point on the AABB that is closest to the circle's center.
		closestPT = temp;
		
		return PointIncCircle(circle, temp);
	}
	
	public static boolean CircleToCircle(CCircle c1, CCircle c2) 
	{
		// Get the squared distance of the vector from the center of each circle.
		// Optimized: Finding the squared distance avoids the use of the square root operator.
		float dist = c1.center.distanceSq(c2.center);
		
		// Get the sum of the radii for both circles
		float sum_radii = c1.radius + c2.radius;
		
		// if the squared distance between the circle's center is less than the squared sum of the radii,
		// then we have a collision and this will return true.
		return (Math.abs(dist) < (sum_radii * sum_radii));
	}
}
